<?php
/**
 * Zendex (Zend (Framework) Extensions)
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://zfx.netatoo.fr/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to support@netatoo.fr so we can send you a copy immediately.
 *
 * @category   Zendex
 * @package    Zendex_Bootstrap
 * @copyright  Copyright (c) 2007 Netatoo FR S.A.R.L. (http://www.netatoo.fr)
 * @license    http://Zendex.netatoo.fr/license/new-bsd     New BSD License
 */

/**
 * @see Zendex_Setup
 */
require_once 'Zendex/Setup.php';

/**
 * @category   Zendex
 * @package    Zendex_Bootstrap
 * @copyright  Copyright (c) 2007 Netatoo FR S.A.R.L. (http://www.netatoo.fr)
 * @license    http://Zendex.netatoo.fr/license/new-bsd     New BSD License
 */
abstract class Zendex_Bootstrap_Dispatcher_Abstract
{    
    /**
     * Parse an xml element and execute method callback if needed
     * 
     * @param string $filename
     * @return void
     */
    protected function _dispatch($xml, $prefix = '', $suffix = '') 
    {
        $dispatchers = Zendex_Bootstrap::getDispatchers();
        
        $plugins = Zendex_Bootstrap::getPlugins();
        $plugins->setXmlElement($xml);
        $plugins->dispatchLoopStartup($xml);
 
        foreach($xml as $tagName => $tagContent) {
            
            $plugins->preDispatchElement($tagName, $tagContent);
            
            $methodName = $prefix . $tagName . $suffix;
            
            if(method_exists($this, $methodName)) {
                Zend_Debug::dump($methodName);
                $this->$methodName($tagContent);
            } elseif(array_key_exists($tagName, $dispatchers)) {

                if(method_exists($dispatchers[$tagName], $methodName)) {
                    Zend_Debug::dump($methodName);
                    $parser = $dispatchers[$tagName];
                    $parser->$methodName($tagContent);
                } 
            }

            $plugins->postDispatchElement($tagName, $tagContent);
                        
        }
        
        $plugins->dispatchLoopShutdown($xml);
    }
        
    /**
     * Merge two arrays recursively, overwriting keys of the same name
     * in $array1 with the value in $array2.
     *
     * @param array $array1
     * @param array $array2
     * @return array
     */
    protected function _arrayMergeRecursive($array1, $array2)
    {
        if (is_array($array1) && is_array($array2)) {
            foreach ($array2 as $key => $value) {
                if (isset($array1[$key])) {
                    $array1[$key] = $this->_arrayMergeRecursive($array1[$key], $value);
                } else {
                    $array1[$key] = $value;
                }
            }
        } else {
            $array1 = $array2;
        }
        return $array1;
    }
    
    /**
     * Parse an xml element and execute method callback if needed
     * 
     * @param string $filename
     * @return void
     */
    protected function _toBoolean($data, $default = false) 
    {
        $data = strtolower( (string) $data );
        
        switch ($data) {
            case true    : return true;        break;
            case false   : return false;     break;
            default      : return $default;     break;
        }
    }
    
    /**
     * Parse an xml element and execute method callback if needed
     * 
     * @param string $filename
     * @return void
     */
    protected function _toString($data, $default = null) 
    {
        return !is_null($data) ? (string) $data : $default;
    }
    
    /**
     * Parse an xml element and execute method callback if needed
     * 
     * @param string $filename
     * @return void
     */
    protected function _toInt($data, $default = 0) 
    {
        $data = (string) $data;
        return !is_null($data) ? (int) $data : $default;
    }
    
    /**
     * Parse an xml element and execute method callback if needed
     * 
     * @param string $filename
     * @return void
     */
    protected function _toArray($data, $default = array(), $testNull = true) 
    {
        if($testNull) {
            return !is_null($data) ? (array) $data : $default;
        } else {
            return $data ? (array) $data : $default;
        }
    }
}